package edu.cicese.alz.swing;

import com.jgoodies.looks.plastic.PlasticLookAndFeel;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SpinnerDateModel;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.text.DefaultFormatter;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 29/05/12
 * Time: 04:24 PM
 */
public class SwingUtil {
	public static final Color cPressed = new Color(255, 216, 107);

	public static final Color cRollover = new Color(252, 226, 134);
	public static final Color cRolloverBorder = new Color(241, 202, 85);
	public static final Color cPressedBorder = new Color(194, 141, 51);
	public static final Color cBackground = new Color(248, 248, 248);

	public static final Color cTextFieldError = new Color(255, 209, 209);

	public static final Color[] colors1 = new Color[]{cBackground, cRollover, cRolloverBorder, cPressedBorder};
	public static final Color[] colors2 = new Color[]{Color.WHITE, cRollover, cRolloverBorder, cPressedBorder};

	public static final LineBorder borderRollover = new LineBorder(cRolloverBorder);
	public static final LineBorder borderPressed = new LineBorder(cPressedBorder);

	public static final CompoundBorder borderColor = BorderFactory.createCompoundBorder(
			BorderFactory.createLineBorder(Color.LIGHT_GRAY), BorderFactory.createLineBorder(Color.WHITE)
	);
	public static final CompoundBorder borderRolloverColor = BorderFactory.createCompoundBorder(
			borderRollover, BorderFactory.createLineBorder(Color.WHITE)
	);
	public static final CompoundBorder borderPressedColor = BorderFactory.createCompoundBorder(
			borderPressed, BorderFactory.createLineBorder(Color.WHITE)
	);

	public static final CompoundBorder borderTextField = BorderFactory.createCompoundBorder(
			BorderFactory.createLineBorder(new Color(212, 214, 217), 1),
			BorderFactory.createEmptyBorder(2, 1, 0, 1));

	public static final CompoundBorder borderTextFieldError = BorderFactory.createCompoundBorder(
			BorderFactory.createLineBorder(Color.RED, 1),
			BorderFactory.createEmptyBorder(2, 1, 0, 1));

	public static final EmptyBorder borderEmpty0 = new EmptyBorder(0, 0, 0, 0);
	public static final EmptyBorder borderEmpty1 = new EmptyBorder(1, 1, 1, 1);

	public static final Insets insetsEmpty = new Insets(0, 0, 0, 0);

	public static final Font fontMain = new Font("Calibri", Font.PLAIN, 13);

	public static final SimpleDateFormat calendarDateFormat = new SimpleDateFormat("dd-MM-yyyy");
	public static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");


	// REPORTS
	public static final Color cSectionBg = new Color(221, 231, 238);
	public static final Color cReportSubsectionBg = new Color(245, 245, 245);
	public static final Color cSectionForeground = new Color(0, 19, 100);
	public static final Color cSectionBorder = new Color(197, 197, 197);
	public static final Font fontSection = new Font("Calibri", Font.BOLD, 13);
	public static final Font fReportType = new Font("Calibri", Font.PLAIN, 15);
	public static final Font fDate = new Font("Calibri", Font.BOLD, 13);
	public static final Font fMap = new Font("Calibri", Font.BOLD, 12);

	// TABLES
//	public static Color cTableHeader = new Color(252, 226, 134);
	public static final Color cTableHeader = new Color(191, 221, 255);
	public static final Font fontTableHeader = new Font("Calibri", Font.BOLD, 14);
	public static final Font fontUserID = new Font("Sans Serif", Font.BOLD, 13);
	public static final Font fontNormal = new Font("Sans Serif", Font.PLAIN, 12);
	public static final Font fontUpdate = new Font("Sans Serif", Font.ITALIC, 12);
	public static final Color colorDelete = new Color(255, 209, 209);
	public static final Color colorInsert = new Color(201, 255, 193);

	public static void setBounds(JComponent component, int x, int y, int width, int height) {
		component.setBounds(x, y, width, height);
	}

	public static void addButtonBackground(JButton button, String text, int width, int height, String toolTipText, Icon icon, Icon rIcon, Icon pIcon, Container container) {
		button.setText(text);
		setSize(button, width, height);
		button.setBackground(cBackground);
		if (toolTipText != null) {
			button.setToolTipText(toolTipText);
		}
		button.setIcon(icon);
		button.setRolloverIcon(rIcon);
		button.setPressedIcon(pIcon);
		button.setContentAreaFilled(false);
		button.setHorizontalTextPosition(JButton.CENTER);
		button.setVerticalTextPosition(JButton.CENTER);
		button.setFocusPainted(false);
		button.setBorder(new EmptyBorder(1, 1, 1, 1));
		button.setMargin(new Insets(0, 0, 0, 0));
		if (container != null) {
			container.add(button);
		}
	}

	public static JButton createButtonBackground(String text, int width, int height, String toolTipText, Icon icon, Icon rIcon, Icon pIcon) {
		JButton button = new JButton();
		button.setText(text);
		setSize(button, width, height);
		button.setBackground(cBackground);
		if (toolTipText != null) {
			button.setToolTipText(toolTipText);
		}
		button.setIcon(icon);
		button.setRolloverIcon(rIcon);
		button.setPressedIcon(pIcon);
		button.setContentAreaFilled(false);
		button.setHorizontalTextPosition(JButton.CENTER);
		button.setVerticalTextPosition(JButton.CENTER);
		button.setFocusPainted(false);
		button.setFocusable(false);
		button.setBorder(borderEmpty1);
		button.setMargin(insetsEmpty);

		return button;
	}


	public static void addButton(JButton button, String text, int width, int height, String toolTipText, Container container) {
		button.setText(text);
		setSize(button, width, height);

		setDefault(button, toolTipText, container);
		addMouseListener(button);
	}

	public static void addButton(JButton button, int x, int y, int width, int height, String toolTipText, Container container) {
		setBounds(button, x, y, width, height);

		setDefault(button, toolTipText, container);
		addMouseListener(button);
	}

	public static void addButton(JButton button, int x, int y, int width, int height, String toolTipText, Icon icon, Container container) {
		setBounds(button, x, y, width, height);
		button.setIcon(icon);

		setDefault(button, toolTipText, container);
		addMouseListener(button);
	}

	public static void addButton(JButton button, String label, int x, int y, int width, int height, String toolTipText, Icon icon, Container container) {
		button.setText(label);
		addButton(button, x, y, width, height, toolTipText, icon, container);
	}

	public static void addButton(JButton button, String label, String toolTipText, Icon icon, Container container) {
		button.setText(label);
		button.setIcon(icon);

		setDefault(button, toolTipText, container);
		addMouseListener(button);
	}


	public static JButton getButton(String label, String toolTipText, Icon icon) {
		JButton button = new JButton();
		button.setText(label);
		if (icon != null) {
			button.setIcon(icon);
		}

		setDefault(button, toolTipText, null);
		addMouseListener(button);

		return button;
	}

	public static JButton getColorButton(int x, int y) {
		return getColorButton(x, y, Color.BLACK);
	}

	public static JButton getColorButton(int x, int y, Color bgColor) {
		JButton button = new JButton();
		setSize(button, x, y);
		button.setFocusPainted(false);
		button.setBackground(bgColor);
		button.setBorder(borderColor);
		button.addMouseListener(new ColorButtonListener(button));

		return button;
	}

	public static JColorButton createColorButton(int x, int y) {
		return createColorButton(x, y, Color.BLACK);
	}

	public static JColorButton createColorButton(int x, int y, Color bgColor) {
		JColorButton button = new JColorButton(bgColor);
		setSize(button, x, y);
		button.setFocusPainted(false);
		button.setBackground(bgColor);
		button.setBorder(borderColor);
		button.addMouseListener(new ColorButtonListener(button));
		button.setFocusable(false);

		return button;
	}

	public static void addToggleButton(final JButton button, String label, String toolTipText, Icon icon, Container container) {
		button.setText(label);
		if (icon != null) {
			button.setIcon(icon);
		}

		if (toolTipText != null) {
			button.setToolTipText(toolTipText);
		}
		button.setBackground(cBackground);
		button.setFocusPainted(false);
		button.setBorder(borderEmpty1);
		button.setMargin(insetsEmpty);
		if (container != null) {
			container.add(button);
		}
	}

	public static void setValues(JButton button, String label, String toolTipText, Icon icon) {
		button.setText(label);
		button.setBackground(cBackground);
		button.setFocusPainted(false);
		button.setBorder(borderEmpty1);
		button.setMargin(insetsEmpty);
		if (icon != null) {
			button.setIcon(icon);
		}
		if (toolTipText != null) {
			button.setToolTipText(toolTipText);
		}
	}

	public static void setDefault(JButton button, String toolTipText, Container container) {
		if (toolTipText != null) {
			button.setToolTipText(toolTipText);
		}
		button.setBackground(cBackground);
		button.setFocusPainted(false);
		button.setFocusable(false);
		button.setBorder(borderEmpty1);
		button.setMargin(insetsEmpty);
		if (container != null) {
			container.add(button);
		}
	}

	public static void addMouseListener(JButton button) {
		addMouseListener(button, SwingUtil.cBackground);
	}

	public static void addMouseListener(final JButton button, final Color bgColor) {
		button.addMouseListener(new MouseAdapter() {
			boolean pressed = false;

			public void mouseEntered(MouseEvent evt) {
				if (button.isEnabled()) {
					if (!pressed) {
						button.setBackground(cRollover);
						button.setBorder(new LineBorder(cRolloverBorder));
					} else {
						button.setBorder(new LineBorder(cPressedBorder));
					}
				}
			}

			public void mouseExited(MouseEvent evt) {
				button.setBackground(bgColor);
				button.setBorder(new EmptyBorder(1, 1, 1, 1));
			}

			public void mousePressed(MouseEvent evt) {
				if (button.isEnabled()) {
					pressed = true;
					button.setBorder(new LineBorder(cPressedBorder));
				}
			}

			public void mouseClicked(MouseEvent evt) {
				if (button.isEnabled()) {
					button.setBorder(new LineBorder(cRolloverBorder));
				}
			}

			public void mouseReleased(MouseEvent evt) {
				if (button.isEnabled() && pressed && insideButton(evt.getX(), evt.getY())) {
					button.setBackground(cRollover);
					button.setBorder(new LineBorder(cRolloverBorder));
				}
				pressed = false;
			}

			private boolean insideButton(int x, int y) {
				return (x >= 0 && x < button.getWidth()) && (y >= 0 && y < button.getHeight());
			}
		});
	}


	public static void setSize(Component component, int width, int height) {
		component.setPreferredSize(new Dimension(width, height));
		component.setMaximumSize(component.getPreferredSize());
		component.setMinimumSize(component.getPreferredSize());
	}


	public static void disableTextField(Component[] comp) {
		for (int x = 0; x < comp.length; x++) {
			if (comp[x] instanceof JPanel) disableTextField(((JPanel) comp[x]).getComponents());
			else if (comp[x] instanceof JTextField) {
				((JTextField) comp[x]).setEditable(false);
				return;
			}
		}
	}

	public static void clearSystemFileChooser(Container parent) {
		for (Component comp : parent.getComponents()) {
			if (comp instanceof JTextField) {
				((JTextField) comp).setText("");
			}
			else if (comp instanceof Container) {
				clearSystemFileChooser((Container) comp);
			}
		}
	}


	public static JLabel createLabel(String text, int width, int height, Font font) {
		JLabel lbl = createLabel(text, width, height);
		lbl.setFont(font);

		return lbl;
	}

	public static JLabel createLabel(String text, int width, int height) {
		JLabel lbl = new JLabel(text);
		lbl.setPreferredSize(new Dimension(width, height));
		lbl.setMaximumSize(lbl.getPreferredSize());

		return lbl;
	}

	public static JLabel createLabel(String text, Font font) {
		JLabel lbl = new JLabel(text);
		lbl.setFont(font);

		return lbl;
	}


	public static JTextField createTextField(int width, int height, int textAlignment, boolean editable) {
		JTextField txt = createTextField(width, height, textAlignment);
		txt.setEditable(editable);
		txt.setFocusable(editable);

		return txt;
	}

	public static JTextField createTextField(int width, int height, int textAlignment) {
		JTextField txt = createTextField(width, height);
		txt.setHorizontalAlignment(textAlignment);

		return txt;
	}

	public static JTextField createTextField(int width, int height) {
		JTextField txt = new JTextField();
		txt.setPreferredSize(new Dimension(width, height));
		txt.setMaximumSize(txt.getPreferredSize());
		txt.setMinimumSize(txt.getPreferredSize());
		txt.setBorder(borderTextField);

		return txt;
	}


	public static JNonEmptyTextField createNonEmptyTextField(int width, int height) {
		JNonEmptyTextField txt = new JNonEmptyTextField();
		txt.setPreferredSize(new Dimension(width, height));
		txt.setMaximumSize(txt.getPreferredSize());
		txt.setBorder(borderTextField);

		return txt;
	}

	public static JNonEmptyTextField createNonEmptyTextField(int width, int height, int textAlignment) {
		JNonEmptyTextField txt = createNonEmptyTextField(width, height);
		txt.setHorizontalAlignment(textAlignment);

		return txt;
	}

	public static JNonEmptyTextField createNonEmptyTextField(int width, int height, int textAlignment, boolean editable) {
		JNonEmptyTextField txt = createNonEmptyTextField(width, height, textAlignment);
		txt.setEditable(editable);
		txt.setFocusable(editable);

		return txt;
	}


	public static JButton createButton(String text, String toolTipText, Icon icon, Font font, int textAlignment) {
		JButton button = createButton(text, toolTipText, icon, font);
		button.setHorizontalTextPosition(textAlignment);

		return button;
	}

	public static JButton createButton(String text, String toolTipText, Icon icon, Font font) {
		JButton button = createButton(text, toolTipText, icon);
		button.setFont(font);

		return button;
	}

	public static JButton createButton(String text, String toolTipText, Icon icon, Color bgColor) {
		JButton button = new JButton();
		button.setText(text);
		if (icon != null) {
			button.setIcon(icon);
		}

		setDefault(button, toolTipText, null);
		addMouseListener(button, bgColor);

		button.setBackground(bgColor);

		return button;
	}

	public static JButton createButton(String text, String toolTipText, Icon icon, Color bgColor, int width, int height) {
		JButton button = createButton(text, toolTipText, icon, bgColor);
		setSize(button, width, height);

		return button;
	}

	public static JButton createButton(String text, String toolTipText, Icon icon, int width, int height) {
		JButton button = createButton(text, toolTipText, icon);
		setSize(button, width, height);

		return button;
	}

	public static JButton createButton(String text, String toolTipText, Icon icon) {
		JButton button = new JButton();
		button.setText(text);
		if (icon != null) {
			button.setIcon(icon);
		}

		setDefault(button, toolTipText, null);
		addMouseListener(button);

		return button;
	}


	public static CustomButton createCustomButton(String text, String toolTipText, Icon icon, Font font, int textAlignment) {
		CustomButton button = createCustomButton(text, toolTipText, icon, font);
		button.setHorizontalTextPosition(textAlignment);

		return button;
	}

	public static CustomButton createCustomButton(String text, String toolTipText, Icon icon, Font font) {
		CustomButton button = createCustomButton(text, toolTipText, icon);
		button.setFont(font);

		return button;
	}

	public static CustomButton createCustomButton(String text, String toolTipText, Icon icon, Color bgColor) {
		CustomButton button = createCustomButton(text, toolTipText, icon);
		button.setBgColor(bgColor);
		button.setBackground(bgColor);

		return button;
	}

	public static CustomButton createCustomButton(String text, String toolTipText, Icon icon, Color bgColor, int width, int height) {
		CustomButton button = createCustomButton(text, toolTipText, icon, bgColor);
		setSize(button, width, height);

		return button;
	}

	public static CustomButton createCustomButton(String text, String toolTipText, Icon icon, int width, int height) {
		CustomButton button = createCustomButton(text, toolTipText, icon);
		setSize(button, width, height);

		return button;
	}

	public static CustomButton createCustomButton(String text, String toolTipText, Icon icon) {
		CustomButton button = new CustomButton(cBackground);
		button.setText(text);
		if (icon != null) {
			button.setIcon(icon);
		}
		if (toolTipText != null) {
			button.setToolTipText(toolTipText);
		}

		setCustomButtonDefaults(button);
		if (!UIManager.getLookAndFeel().getName().equals("Windows")) {
			button.addMouseListener(new CustomButtonListener(button));
		}

		return button;
	}

	public static void setCustomButtonDefaults(AbstractButton button) {
		button.setFocusPainted(false);
		button.setFocusable(false);
		button.setBorder(borderEmpty1);
		button.setMargin(insetsEmpty);
	}

	// TODO Replace!
	public static JToggleButton createToggleButton(String text, String toolTipText, Icon icon) {
		JToggleButton button = new JToggleButton();
		button.setText(text);
		if (icon != null) {
			button.setIcon(icon);
		}
		if (toolTipText != null) {
			button.setToolTipText(toolTipText);
		}

		button.setContentAreaFilled(false);
		button.setFocusPainted(false);
		button.setBorder(new EmptyBorder(3, 3, 3, 3));
		button.setMargin(insetsEmpty);
		button.addMouseListener(new TypeButtonListener(button));

		return button;
	}

	public static JToggleButton createToggleButton(String text, String toolTipText, Icon icon, boolean selected) {
		JToggleButton button = new JToggleButton();
		button.setText(text);
		button.setSelected(selected);
		if (icon != null) {
			button.setIcon(icon);
		}
		if (toolTipText != null) {
			button.setToolTipText(toolTipText);
		}

		setCustomButtonDefaults(button);

		return button;
	}

	public static ColoredToggleButton createColoredToggleButton(String text, int width, int height,
	                                                            Icon icon, String toolTipText,
	                                                            boolean selected, Color backgroundColor) {
		ColoredToggleButton button = new ColoredToggleButton(selected, backgroundColor);
		button.setText(text);
		if (icon != null) {
			button.setIcon(icon);
		}
		if (toolTipText != null) {
			button.setToolTipText(toolTipText);
		}
		button.setFocusPainted(false);
		button.setBorder(borderEmpty1);
		button.setMargin(insetsEmpty);
		setSize(button, width, height);

		return button;
	}

	public static ColoredToggleButton createColoredToggleButton(String text, int width, int height, Icon icon,
	                                                            String toolTipText, boolean selected) {
		return createColoredToggleButton(text, width, height, icon, toolTipText, selected, SwingUtil.cBackground);
	}

	public static CustomToggleButton createCustomToggleButton(String text, String toolTipText, Icon icon, boolean selected, Color bgColor) {
		CustomToggleButton button = new CustomToggleButton(selected, bgColor);
		button.setText(text);
		if (icon != null) {
			button.setIcon(icon);
		}
		if (toolTipText != null) {
			button.setToolTipText(toolTipText);
		}
		button.setFocusPainted(false);
		button.setBorder(borderEmpty1);
		button.setMargin(insetsEmpty);

		if (selected) {
			button.paintSelected();
		} else {
			button.paintDeselected();
		}

		return button;
	}

	public static CustomToggleButton createCustomToggleButton(String text, String toolTipText, Icon icon, boolean selected, Color bgColor, int width, int height) {
		CustomToggleButton button = new CustomToggleButton(selected, bgColor);
		button.setText(text);
		if (icon != null) {
			button.setIcon(icon);
		}
		if (toolTipText != null) {
			button.setToolTipText(toolTipText);
		}
		button.setFocusPainted(false);
		button.setBorder(borderEmpty1);
		button.setMargin(insetsEmpty);

		if (selected) {
			button.paintSelected();
		} else {
			button.paintDeselected();
		}

		setSize(button, width, height);

		return button;
	}


	public static JRadioButton createRadioButton(String text) {
		JRadioButton btn = new JRadioButton(text) {
			@Override
			public void paintBorder(Graphics g) {
				if (isFocusOwner()) {
					Border border = new DashedBorder();
					border.paintBorder(this, g, 15, 0, getWidth() - 15, getHeight());
				}
			}
		};
		btn.setOpaque(false);
		btn.setFocusPainted(false);

		btn.setMaximumSize(new Dimension(btn.getPreferredSize().width, 20));

		return btn;
	}

	public static JRadioButton createRadioButton(String text, int width, int height) {
		JRadioButton btn = createRadioButton(text);
		setSize(btn, width, height);

		return btn;
	}


	public static JComboBox createComboBox(Object[] data, int width, int height) {
		JComboBox comboBox = new JComboBox(data);
//		BasicComboBoxUI basicComboBoxUI = new BasicComboBoxUI();
		comboBox.setPreferredSize(new Dimension(width, height));
		comboBox.setMaximumSize(comboBox.getPreferredSize());
		comboBox.setMinimumSize(comboBox.getPreferredSize());
		comboBox.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createLineBorder(new Color(212, 214, 217), 1),
				BorderFactory.createEmptyBorder(0, 0, 0, 0)));
//		comboBox.setFocusable(false);
		comboBox.setFont(fontMain);
		comboBox.setAlignmentX(Component.LEFT_ALIGNMENT);
		/*comboBox.setRenderer(new DefaultListCellRenderer() {
			@Override
			public Component getListCellRendererComponent(JList list, Object value,
			                                              int index, boolean isSelected, boolean cellHasFocus) {

				Component comp = super.getListCellRendererComponent(list,
						value, index, isSelected, cellHasFocus);

				return comp;

//				if (-1 < index && null != value && null != tooltips) {
//					list.setToolTipText(tooltips.get(index));
//				}
//				return comp;
			}

			@Override
			public void paint(Graphics g) {
				setBackground(Color.WHITE);
				super.paint(g);
			}
		});
*/
		comboBox.setUI(new BasicComboBoxUI());

		return comboBox;
	}


	public static JCheckBox createCheckBox(String text) {
		JCheckBox checkBox = new JCheckBox(text) {
			@Override
			public void paintBorder(Graphics g) {
				if (isFocusOwner()) {
					Border border = new DashedBorder();
					border.paintBorder(this, g, 15, 0, getWidth() - 15, getHeight());
				}
			}
		};
		
		checkBox.setOpaque(false);
		checkBox.setFocusPainted(false);

		checkBox.setPreferredSize(new Dimension(checkBox.getPreferredSize().width, 20));
		checkBox.setMaximumSize(new Dimension(checkBox.getPreferredSize().width, 20));

		return checkBox;
	}

	public static JCheckBox createCheckBox(String text, int width, int height) {
		JCheckBox checkBox = createCheckBox(text);
		setSize(checkBox, width, height);

		return checkBox;
	}


	public static JSpinner createSpinner(Date date, SpinnerDateModel model) {
		JSpinner spinner = new JSpinner(model);
		spinner.setFont(SwingUtil.fontMain);
		spinner.setBackground(Color.WHITE);
		spinner.setBorder(borderTextField);
		spinner.setMaximumSize(new Dimension(72, 20));
		JSpinner.DateEditor dateEditor = new JSpinner.DateEditor(spinner, "HH:mm:ss");
		spinner.setEditor(dateEditor);

//		JComponent comp = spinner.getEditor();
//		JFormattedTextField field = (JFormattedTextField) comp.getComponent(0);
//		DefaultFormatter formatter = (DefaultFormatter) field.getFormatter();
//		formatter.setCommitsOnValidEdit(true);

		spinner.setValue(date);

		return spinner;
	}


	public static JSlider createSlider(int orientation, int min, int max, int value, int width, int height) {
		JSlider slider = new JSlider(orientation, min, max, value);
		slider.setOpaque(false);
		slider.setPreferredSize(new Dimension(width, height));
		slider.setMaximumSize(slider.getPreferredSize());
		slider.setToolTipText(slider.getValue() + "");

		return slider;
	}

	public static JSlider createSlider(int orientation, int min, int max, int value, int x, int y, int width, int height) {
		JSlider slider = new JSlider(orientation, min, max, value);
		slider.setOpaque(false);
		slider.setBounds(x, y, width, height);

		return slider;
	}


	public static void setTextFieldError(JNonEmptyTextField textField, boolean error) {
		textField.setError(error);
		if (error) {
			textField.setBorder(borderTextFieldError);
			textField.setBackground(cTextFieldError);
		} else {
			textField.setBorder(borderTextField);
			textField.setBackground(Color.WHITE);
		}
	}

	public static void checkEmptyTextField(JNonEmptyTextField textField, boolean enabled) {
		setTextFieldError(textField, textField.getText().trim().equals("") && enabled);
	}


	public static Color getTransparentColor(Color color, int opacity) {
		int R = color.getRed();
		int G = color.getGreen();
		int B = color.getBlue();
		return new Color(R, G, B, opacity);
	}


	public static String convertToMultiline(String text) {
		return "<html>" + text.replaceAll("\n", "<br>");
	}


	public static void setUIManager() {
		JComponent.setDefaultLocale(Locale.ENGLISH);

		//		PlasticLookAndFeel.setPlasticTheme(new DesertBlue());
		try {
			UIManager.put("PopupMenu.consumeEventOnClose", Boolean.TRUE);

//			UIManager.setLookAndFeel(new MetalLookAndFeel());
			UIManager.setLookAndFeel(new PlasticLookAndFeel());
//			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//			System.out.println(UIManager.getSystemLookAndFeelClassName());

			UIManager.put("Panel.background", Color.WHITE);
			UIManager.put("OptionPane.background", Color.WHITE);
			UIManager.put("TextArea.inactiveBackground", Color.WHITE);
			UIManager.put("ProgressBar.selectionBackground", Color.DARK_GRAY);
			UIManager.put("ProgressBar.selectionForeground", cBackground);

			UIManager.put("ToolTip.background", new Color(255, 255, 225));
			UIManager.put("ToolTip.foreground", Color.DARK_GRAY);
			UIManager.put("ToolTip.border", BorderFactory.createCompoundBorder(
					BorderFactory.createLineBorder(new Color(64, 64, 64), 1),
					BorderFactory.createEmptyBorder(2, 0, 0, 0)));
			Font tooltipFont = new Font("Calibri", Font.BOLD, 12);
			UIManager.put("ToolTip.font", tooltipFont);

			UIManager.put("Button.select", cPressed);
			UIManager.put("Button.borderPaintsFocus", false);
			UIManager.put("Button.font", fontMain);
			UIManager.put("Button.background", Color.WHITE);

			UIManager.put("Table.background", Color.WHITE);
			UIManager.put("Table.gridColor", new Color(212, 214, 217));
			UIManager.put("Table.font", fontMain);
			UIManager.put("Table.selectionBackground", cPressed);
			UIManager.put("Table.selectionForeground", Color.BLACK);
			UIManager.put("Table.focusCellHighlightBorder", BorderFactory.createLineBorder(cPressedBorder));

			UIManager.put("List.background", Color.WHITE);
			UIManager.put("List.selectionBackground", cPressed);
			UIManager.put("List.selectionForeground", Color.BLACK);
			UIManager.put("List.border", borderEmpty0);
			UIManager.put("List.focusCellHighlightBorder", borderEmpty1);
			UIManager.put("List.font", fontMain);

			UIManager.put("Table.border", borderEmpty0);

			UIManager.put("ScrollPane.border", new LineBorder(Color.LIGHT_GRAY, 1));
			UIManager.put("ScrollPane.background", cBackground);

			UIManager.put("Label.font", fontMain);

			UIManager.put("ToolBar.background", cBackground);
			UIManager.put("ToolBar.border", borderEmpty0);

			UIManager.put("RadioButton.font", fontMain);
//			UIManager.put("RadioButton.background", Color.WHITE);

			UIManager.put("CheckBox.font", fontMain);
//			UIManager.put("CheckBox.background", Color.WHITE);

			UIManager.put("TextField.font", fontMain);

			UIManager.put("ComboBox.background", new ColorUIResource(Color.WHITE));
			UIManager.put("ComboBox.selectionBackground", new ColorUIResource(cRollover));
	        UIManager.put("ComboBox.selectionForeground", Color.BLACK);

			UIManager.put("MenuItem.background", cBackground);
//			UIManager.put("PopupMenu.foreground", cBackground);
			UIManager.put("MenuItem.selectionBackground", cRollover);
//			UIManager.put("PopupMenu.selectionForeground", cBackground);

//			UIManager.put("ComboBox.background", SwingUtil.cBackground);
//			UIManager.put("ComboBox.selectionBackground", SwingUtil.cRollover);
//			UIManager.put("ComboBox.selectionForeground", Color.BLACK);
//
//			UIManager.put("CheckBoxMenuItem.background", Color.RED);
//			UIManager.put("CheckBoxMenuItem.select", Color.YELLOW);

		} catch (Exception e) {
			e.printStackTrace();
		}
//		PlasticLookAndFeel.setPlasticTheme(new DesertBlue());
	}


	static class ColorButtonListener extends MouseAdapter {
		private JButton button;

		public ColorButtonListener(JButton button) {
			this.button = button;
		}

		public void mouseEntered(MouseEvent evt) {
			if (button.isEnabled()) {
				button.setBorder(borderRolloverColor);
			}
		}

		public void mouseExited(MouseEvent evt) {
			button.setBorder(borderColor);
		}

		public void mousePressed(MouseEvent evt) {
			if (button.isEnabled()) {
				button.setBorder(borderPressedColor);
			}
		}

		public void mouseClicked(MouseEvent evt) {
			if (button.isEnabled()) {
				button.setBorder(borderRolloverColor);
			}
		}
	}

	static class TypeButtonListener extends MouseAdapter {
		private JToggleButton button;

		public TypeButtonListener(JToggleButton button) {
			this.button = button;
		}

		public void mouseEntered(MouseEvent evt) {
			if (button.isEnabled()) {
				button.setBorder(new LineBorder(cRolloverBorder, 3));
			}
		}

		public void mouseExited(MouseEvent evt) {
			if (!button.isSelected()) {
				button.setBorder(new EmptyBorder(3, 3, 3, 3));
			}
		}

		public void mousePressed(MouseEvent evt) {
			if (button.isEnabled()) {
				button.setBorder(new LineBorder(cPressedBorder, 3));
			}
		}

		public void mouseClicked(MouseEvent evt) {
			if (button.isEnabled()) {
//				setSelected(!isSelected());
//					selected = !selected;
				button.setBorder(new LineBorder(cRolloverBorder, 3));
//				if (button.isSelected()) {
//				}
//				else {
//					button.setBorder(new LineBorder(cPressedBorder, 3));
//				}
			}
		}
	}
}
